<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：互动折扣王-连环折扣-倒计时折扣 
// 代码日期：v3.0 2017-7-12
// 作者：杨道泉 
// 文件名：QnuiTmcAction.class.php 
// 功能： 操作订单通知

class QnuiTmcController extends QnuiBaseController {
	public function get_state(){
		$TMC = M("TmcStatus");
		$status = $TMC->where(Array("sid"=>session("SHOP_ID")))->getField("status");
		if(false===$status){
			$tmc_user = Array(
				"sid"=>session("SHOP_ID"),
				"user_nick"=>session("VISITOR_NICK"),
				"taobao_user_id"=>session("VISITOR_ID"),
				"sessionkey"=>session("TOP_SESSION"),
				"status"=>0);
			if(session("VIP")){
				$tmc_user["is_vip"]=1;
			}
			$TMC->add($tmc_user);
			$status=0;
		}
		$resp = new \StdClass();
		$resp->status=$status;
		$this->ajaxReturn(pre_resp($resp));
	}
	public function state(){
		$user_nick = session("VISITOR_NICK");

		Vendor('taobaosdk/top/request/TmcUserGetRequest');
		$req = new \TmcUserGetRequest;
		$req->setFields("topics,is_valid,created,modified,group_name,user_nick,user_id");
		$req->setNick($user_nick);
		// $req->setUserPlatform("tbUIC");
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$resp=(Array)$resp;
		$resp["topics"]=C("NormalTopics");
		$this->ajaxReturn($resp);
	}
	public function permit(){
		$user_nick = session("VISITOR_NICK");
		$topics = I("topics");
		
		Vendor('taobaosdk/top/request/TmcUserPermitRequest');//taobao.tmc.user.permit 
		$req = new \TmcUserPermitRequest;
		$req->setTopics($topics);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		if($resp->is_success){
			$TMC = M("TmcStatus");
			$TMC->where(Array("sid"=>session("SHOP_ID")))->save(Array("status"=>1));
		}
		$this->ajaxReturn($resp);
	}
	public function cancle(){
		$user_nick = session("VISITOR_NICK");
		
		Vendor('taobaosdk/top/request/TmcUserCancelRequest');//taobao.tmc.user.cancel
		$req = new \TmcUserCancelRequest;
		$req->setNick($user_nick);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		if($resp->code==200){
			$TMC = M("TmcStatus");
			$TMC->where(Array("sid"=>session("SHOP_ID")))->save(Array("status"=>0));
		}
		
		$this->ajaxReturn($resp);
	}
	public function topics(){
		$cat = I("cat");
		if($cat=="all"){
			$topics = C("Topics");
		}
		if($cat=="normal"){
			$topics = C("NormalTopics");
		}
		$resp = new \StdClass();
		$resp->topics = $topics;
		$this->ajaxReturn(pre_resp($resp));
		
	}
}